import bcrypt from 'bcryptjs';

import UserDao from '../../dao/system/UserDao';
import DataEntity from '../../entity/DataEntity';
import User, { UserStatus } from '../../entity/system/User';
import BaseService from '../BaseService';

export default class UserService extends BaseService<User> {
    protected getFileds(): string[] {
        return ['_account', '_name', '_phone', '_sex', '_status', '_remork'];
    }
    protected beforeSave(data: User) {
        data.status = UserStatus.NOMAL;
        let salt = bcrypt.genSaltSync(10);
        data.password = bcrypt.hashSync(data.password, salt);
    }
    protected beforeUpdate<K extends DataEntity>(data: K) {
    }
    constructor() {
        super(new UserDao())
    }
    /**
     * 通过账号查找对应的用户
     * @param account  账号信息
     */
    async findByAccount(account: string): Promise<User | null> {
        return (<UserDao>this._dao).findByAccount(account);
    }
    /**
    * 通过电话号码查找对应的用户
    * @param account  电话号码
    */
    async findByPhone(phone: string): Promise<User | null> {
        return (<UserDao>this._dao).findByPhone(phone);
    }

}
